

use "$path\datasets\rd_dataset.dta", clear
keep if in_rdd_sample==1


preserve
* identify municpipalities in Minutes sample
use "$path\datasets\minutes_data.dta", clear
bysort gkz jahr: keep if _n==1
keep gkz jahr
tempfile municip_in_minutes
save `municip_in_minutes', replace
restore


merge m:1 gkz jahr using `municip_in_minutes'
gen in_protokolle_sample=( _merge==3)
bysort gkz: egen m_in_protokolle_sample=mean(in_protokolle_sample)
replace m_in_protokolle_sample=1 if m_in_protokolle_sample>0 & m_in_protokolle_sample<.
drop _merge




bysort gkz jahr: keep if _n==1
drop log_* 
foreach d of varlist   bevoelkerung  unter_14jahre ueber65jahre von15bis65jahre totrev_pc ts_pc sum_elected_hcd  tot_share_women_elected cdu_seat_share   spd_seat_share{
	bysort gkz:	egen mean_`d'=mean(`d')
	gen log_`d'=log(mean_`d')
	}


bysort gkz: keep if _n==1


estpost sum  log_bevoelkerung log_unter_14jahre log_ueber65jahre log_von15bis65jahre log_totrev_pc log_ts_pc mean_sum_elected_hcd  mean_tot_share_women_elected mean_cdu_seat_share   mean_spd_seat_share    if m_in_protokolle_sample==0
matrix meanf1=e(mean)
matrix list meanf1
estpost sum  log_bevoelkerung log_unter_14jahre log_ueber65jahre log_von15bis65jahre log_totrev_pc log_ts_pc mean_sum_elected_hcd  mean_tot_share_women_elected mean_cdu_seat_share   mean_spd_seat_share     if m_in_protokolle_sample==1
matrix meanf2=e(mean)
matrix list meanf2


estpost ttest  log_bevoelkerung log_unter_14jahre log_ueber65jahre log_von15bis65jahre log_totrev_pc log_ts_pc mean_sum_elected_hcd  mean_tot_share_women_elected mean_cdu_seat_share   mean_spd_seat_share    , by(m_in_protokolle_sample)
estadd matrix meanf1
estadd matrix meanf2



esttab  using TableA31/tableA31.txt, replace order(   log_bevoelkerung log_unter_14jahre log_ueber65jahre log_von15bis65jahre log_totrev_pc log_ts_pc    ) cells("meanf1(fmt(3))  meanf2(fmt(3))   b(star fmt(3)) se(fmt(3)) count(fmt(0))") star(* 0.1 ** .05 *** 0.01) ///
  collabels(  "Not in sample"  "In sample" "Diff." "Std. Error" "Obs.") ///
  varlabels(log_bevoelkerung "Log(Population)" log_unter_14jahre "Log(Population $<$ 14)"  log_ueber65jahre "Log(Population $>$ 65)"  log_von15bis65jahre "Log(Population $>$14 \& $<$ 65)"  log_totrev_pc "Log(Revenues p.c.)"   log_ts_pc "Log(Transfers p.c.)" sum_elected_hcd "Council size"  tot_share_women_elected "\% Women" cdu_seat_share "\% CSU"   spd_seat_share "\% SPD" mean_sum_elected_hcd "Council size"  mean_tot_share_women_elected "\% Women" mean_cdu_seat_share "\% CSU"   mean_spd_seat_share "\% SPD" mean_left_seat_share "\% Left-wing parties" ) 



